﻿using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WPF.DAAB.Client.Classes
{
    public sealed class PersonaRepository
    {
        #region Singleton
        private static readonly PersonaRepository instance = new PersonaRepository();
        private PersonaRepository() { }
        public static PersonaRepository Instance
        {
            get { return instance; }
        }
        #endregion
        #region Methods
        public object ContarPersonas()
        {
            return DatabaseFactory.CreateDatabase().ExecuteScalar("DAAB.Persona_Contar_SP");
        }
        public IEnumerable<Persona> ListarPersonas()
        {
            var database = DatabaseFactory.CreateDatabase("AdventureWorks2012");
            using (IDataReader dataReader = database.ExecuteReader("DAAB.Persona_Listar_SP", 22))
            {
                while (dataReader.Read())
                    yield return new Persona
                    {
                        ID = (int)dataReader["BusinessEntityID"],
                        Nombre = (string)dataReader["FirstName"]
                    };
            }
        }
        public Persona ObtenerPersona(int id)
        {
            using (IDataReader dataReader = DatabaseFactory.CreateDatabase().ExecuteReader("DAAB.Persona_Leer_SP", id))
            {
                if (dataReader.Read())
                    return new Persona
                    {
                        ID = id,
                        SegundoNombre = (string)dataReader["MiddleName"],
                        Apellido = (string)dataReader["LastName"]
                    };
            }
            return null;
        }
        public void ActualizarPersona(Persona persona)
        {
            var database = DatabaseFactory.CreateDatabase("AdventureWorks2012");
            database.ExecuteNonQuery("DAAB.Persona_Actualizar_SP", persona.ID, persona.Apellido);
        }
        #endregion
    }
}
